<template>
  <div class="personal-center">
    <header class="header">
      <div class="logo">AI-智周万物</div>
      <div class="user-info">
        <span class="username">{{ username }}</span>
        <button @click="handleLogout" class="logout-btn">退出登录</button>
      </div>
    </header>

    <main class="content">
      <div class="user-profile">
        <div class="avatar">{{ username.charAt(0) }}</div>
        <div class="profile-info">
          <h2>{{ username }}</h2>
          <p>AI聊天助手用户</p>
        </div>
      </div>

      <div class="action-buttons">
        <button @click="goToChat" class="primary-btn">开始AI聊天</button>
        <button class="secondary-btn">查看使用帮助</button>
      </div>

      <div class="history-section">
        <h3>最近聊天记录</h3>
        <div v-if="chatHistory.length > 0" class="history-list">
          <div v-for="(history, index) in chatHistory" :key="index" class="history-item" @click="goToChat(history.id)">
            <div class="history-title">{{ history.title }}</div>
            <div class="history-time">{{ history.time }}</div>
          </div>
        </div>
        <div v-else class="no-history">暂无聊天记录</div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const username = ref('');
const chatHistory = ref([]);

onMounted(() => {
  // 检查登录状态
  const isLoggedIn = localStorage.getItem('isLoggedIn');
  if (!isLoggedIn) {
    router.push('/login');
    return;
  }

  // 模拟用户数据
  username.value = '测试用户';

  // 模拟聊天历史数据
  chatHistory.value = [
    {
      id: 1,
      title: '关于AI技术的讨论',
      time: '今天 10:30'
    },
    {
      id: 2,
      title: '项目规划咨询',
      time: '昨天 16:45'
    },
    {
      id: 3,
      title: '学习资源推荐',
      time: '2023-06-10 09:15'
    }
  ];
});

const handleLogout = () => {
  localStorage.removeItem('isLoggedIn');
  router.push('/login');
};

const goToChat = (chatId = null) => {
  router.push({ name: 'Chat', params: { chatId } });
};
</script>

<style scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 2rem;
  background-color: #4285f4;
  color: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.logout-btn {
  padding: 0.5rem 1rem;
  background-color: rgba(255, 255, 255, 0.2);
  border: none;
  border-radius: 4px;
  color: white;
  cursor: pointer;
  transition: background-color 0.3s;
}

.logout-btn:hover {
  background-color: rgba(255, 255, 255, 0.3);
}

.content {
  max-width: 800px;
  margin: 2rem auto;
  padding: 0 1rem;
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #eee;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background-color: #4285f4;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
  font-weight: bold;
}

.profile-info h2 {
  margin: 0 0 0.5rem 0;
  color: #333;
}

.profile-info p {
  margin: 0;
  color: #666;
}

.action-buttons {
  display: flex;
  gap: 1rem;
  margin-bottom: 2rem;
}

.primary-btn {
  padding: 0.8rem 1.5rem;
  background-color: #4285f4;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
}

.primary-btn:hover {
  background-color: #3367d6;
}

.secondary-btn {
  padding: 0.8rem 1.5rem;
  background-color: white;
  color: #4285f4;
  border: 1px solid #4285f4;
  border-radius: 5px;
  font-size: 1rem;
  cursor: pointer;
  transition: background-color 0.3s;
}

.secondary-btn:hover {
  background-color: #f0f7ff;
}

.history-section h3 {
  margin-bottom: 1rem;
  color: #333;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.history-item {
  padding: 1rem;
  border: 1px solid #eee;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.history-item:hover {
  background-color: #f5f5f5;
}

.history-title {
  font-weight: 500;
  margin-bottom: 0.25rem;
}

.history-time {
  font-size: 0.8rem;
  color: #666;
}

.no-history {
  text-align: center;
  padding: 2rem;
  color: #999;
  background-color: #f9f9f9;
  border-radius: 5px;
}
</style>